﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedGenomics.Core
{
    public static class TimeService
    {
        private static Func<DateTime> _dateTimeNow = () => DateTime.Now;

        static TimeService()
        {
            Now = _dateTimeNow;
        }

        public static Func<DateTime> Now { get; private set; }

        public static IDisposable Freeze(DateTime time)
        {
            //set the function returned by the static Now property
            //when the return value from this method is disposed, the func will be reset to use DateTime.Now
            Now = () => time;
            return new ResetNowFunc();
        }

        #region ResetNowFunc

        private class ResetNowFunc : IDisposable
        {
            public void Dispose()
            {
                Now = _dateTimeNow;
            }
        }

        #endregion
    }
}
